package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;

import java.io.IOException;

public class GDCharUID {
    private byte[] uid;

    public GDCharUID() {
        this.uid = new byte[16];
    }

    public GDCharUID(final byte[] uid) {
        this.uid = uid;
        if (uid.length != 16) {
            this.uid = new byte[16];
        }
    }

    @Override
    public boolean equals(final Object o) {
        if (o == null) {
            return false;
        }
        if (o.getClass() != GDCharUID.class) {
            return false;
        }
        final GDCharUID guid = (GDCharUID) o;
        for (int i = 0; i < this.uid.length; ++i) {
            if (this.uid[i] > guid.uid[i]) {
                return false;
            }
            if (this.uid[i] < guid.uid[i]) {
                return false;
            }
        }
        return true;
    }

    public void read() throws IOException {
        for (int i = 0; i < this.uid.length; ++i) {
            this.uid[i] = GDReader.readEncByte();
        }
    }

    public void write() throws IOException {
        for (int i = 0; i < this.uid.length; ++i) {
            GDWriter.writeByte(this.uid[i]);
        }
    }

    public static int getByteSize() {
        return 16;
    }

    public void println() {
        String s = "";
        for (int i = 0; i < this.uid.length; ++i) {
            String b;
            for (b = Byte.toString(this.uid[i]); b.length() < 4; b = " " + b) {
            }
            if (i == 0) {
                s = b;
            } else {
                s = s + ", " + b;
            }
        }
        System.out.println(s);
    }
}
